package zbxinter

import (
	"fmt"
	"net/http"
	"strings"
	"webinter/myalert"
	"webinter/mytool"

	"github.com/gin-gonic/gin"
)

type ZABBIXMSG struct {
	Fields struct {
		To      string `json:"To"`
		Subject string `json:"Subject"`
		Message string `json:"Message"`
	} `json:"fields"`
}

func Inter(c *gin.Context) {
	var msg ZABBIXMSG
	err := c.BindJSON(&msg)
	machineconf := mytool.AllConf.MachineConf
	if machineconf.Enabled == true {
		myalert.SendMachine(machineconf.Url, msg.Fields.Subject+"\n"+msg.Fields.Message)
	}
	emailConf := mytool.AllConf.EmailConf
	if emailConf.Enabled == true {
		sendtos := strings.Split(msg.Fields.To, ",")
		myalert.SendEmail(emailConf.Smtpaddr, emailConf.From, emailConf.Password, sendtos,
			msg.Fields.Subject, msg.Fields.Message)
	}
	dingConf := mytool.AllConf.DingConf
	if dingConf.Enabled == true {
		myalert.SendDing(dingConf.Corpid, dingConf.Corpsecret, dingConf.Agentid,
			msg.Fields.To, msg.Fields.Subject+"\n"+msg.Fields.Message)
	}
	wxConf := mytool.AllConf.WxConf
	if wxConf.Enabled == true {
		myalert.SendWechart(wxConf.Corpid, wxConf.Corpsecret, wxConf.Agentid,
			msg.Fields.To, msg.Fields.Subject+"\n"+msg.Fields.Message)
	}
	mytool.CheckErr(err)
	fmt.Println(msg.Fields)
	c.String(http.StatusOK, "ok")
}
